home *** CD-ROM | disk | FTP | other *** search
/ Precision Software Appli…tions Silver Collection 1 / Precision Software Applications Silver Collection Volume One (PSM) (1993).iso / windows / games / winpcx2.arj / WSZ.C < prev    next >
Text File  |  1987-07-26  |  7KB  |  260 lines

  1. /*  whatsize -- Windows application in C  */
  2.  
  3. #include <windows.h>
  4. #include <stdio.h>
  5. #include "wsz.h"
  6.  
  7. long FAR PASCAL MainWndProc(HWND, unsigned, WORD, LONG);
  8. BOOL FAR PASCAL AboutWndProc(HWND, unsigned, WORD, LONG);
  9. BOOL FAR PASCAL UnitsWndProc(HWND, unsigned, WORD, LONG);
  10. BOOL Initialize(HANDLE, HANDLE, LPSTR, int);
  11.  
  12. FARPROC lpAbout;
  13. FARPROC lpUnits;
  14. HANDLE hInst;
  15. int CurrentUnits = ID_PIXELS;
  16. int CurrentColor = ID_BLACK;
  17.  
  18. int PASCAL WinMain(hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  19. HANDLE hInstance, hPrevInstance;
  20. LPSTR lpszCmdLine;
  21. int nCmdShow;
  22. {
  23.     MSG msg;
  24.  
  25.     if (!Initialize(hInstance, hPrevInstance, lpszCmdLine, nCmdShow))
  26.         return FALSE;
  27.  
  28.     while (GetMessage((LPMSG)&msg, NULL, 0, 0)) {
  29.         TranslateMessage((LPMSG)&msg);
  30.         DispatchMessage((LPMSG)&msg);
  31.     }
  32.     return (msg.wParam);
  33. }
  34.  
  35. BOOL Initialize(hInstance, hPrevInst, lpszCmdLine, nCmdShow)
  36. HANDLE hInstance, hPrevInst;
  37. LPSTR lpszCmdLine;
  38. int nCmdShow;
  39. {
  40.     char *szAppName[10];
  41.     char *szAbout[10];
  42.     char *szTitle[30];
  43.     WNDCLASS wndclass;
  44.     HWND hWnd;
  45.     HMENU hMenu;
  46.  
  47.     LoadString(hInstance, IDS_NAME, (LPSTR)szAppName, 10);
  48.     LoadString(hInstance, IDS_ABOUT, (LPSTR)szAbout, 10);
  49.     LoadString(hInstance, IDS_TITLE, (LPSTR)szTitle, 30);
  50.  
  51.     if (!hPrevInst) {
  52.         wndclass.style = CS_HREDRAW | CS_VREDRAW;
  53.         wndclass.lpfnWndProc = MainWndProc;
  54.         wndclass.cbClsExtra = 0;
  55.         wndclass.cbWndExtra = 0;
  56.         wndclass.hInstance = hInstance;
  57.         wndclass.hIcon = LoadIcon(hInstance, (LPSTR)szAppName);
  58.         wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  59.         wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  60.         wndclass.lpszMenuName = (LPSTR)szAppName;
  61.         wndclass.lpszClassName = (LPSTR)szAppName;
  62.         if (!RegisterClass((LPWNDCLASS)&wndclass))
  63.             return FALSE;
  64.     }
  65.     else {
  66.         GetInstanceData(hPrevInst, (NPSTR)&CurrentUnits, sizeof(int));
  67.         GetInstanceData(hPrevInst, (NPSTR)&CurrentColor, sizeof(int));
  68.     }
  69.     hWnd = CreateWindow((LPSTR)szAppName, (LPSTR)szTitle, WS_TILEDWINDOW,
  70.               0,0,0,0,(HWND)NULL, (HMENU)NULL, (HANDLE)hInstance, (LPSTR)NULL);
  71.     hInst = hInstance;
  72.     lpAbout = MakeProcInstance((FARPROC)AboutWndProc, hInstance);
  73.     lpUnits = MakeProcInstance((FARPROC)UnitsWndProc, hInstance);
  74.     hMenu = GetSystemMenu(hWnd, FALSE);
  75.     ChangeMenu(hMenu, 0, NULL, 999, MF_APPEND | MF_SEPARATOR);
  76.     ChangeMenu(hMenu, 0, (LPSTR)szAbout, IDM_ABOUT, MF_APPEND | MF_STRING);
  77.     hMenu = GetMenu(hWnd);
  78.     CheckMenuItem(hMenu, CurrentColor, MF_CHECKED);
  79.     ShowWindow(hWnd, nCmdShow);
  80.     UpdateWindow(hWnd);
  81.     return TRUE;
  82. }
  83.  
  84. long FAR PASCAL MainWndProc(hWnd, message, wParam, lParam)
  85. HWND hWnd;
  86. unsigned message;
  87. WORD wParam;
  88. LONG lParam;
  89. {
  90.     HMENU hMenu;
  91.     static int CharHorzPix, CharVertPix;
  92.     static int ScrnHorzPix, ScrnVertPix;
  93.     static int ScrnHorzMil, ScrnVertMil;
  94.     static int WindHorzPix, WindVertPix;
  95.  
  96.     switch (message) {
  97.         case WM_CREATE:
  98.             {
  99.                 TEXTMETRIC tm;
  100.                 HDC hDC = GetDC(hWnd);
  101.  
  102.                 GetTextMetrics(hDC, (LPTEXTMETRIC)&tm);
  103.                 CharHorzPix = tm.tmAveCharWidth;
  104.                 CharVertPix = tm.tmHeight;
  105.                 ScrnHorzPix = GetDeviceCaps(hDC, HORZRES);
  106.                 ScrnVertPix = GetDeviceCaps(hDC, VERTRES);
  107.                 ScrnHorzMil = GetDeviceCaps(hDC, HORZSIZE);
  108.                 ScrnVertMil = GetDeviceCaps(hDC, VERTSIZE);
  109.                 ReleaseDC(hWnd, hDC);
  110.             }
  111.             break;
  112.     
  113.         case WM_SIZE:
  114.             WindHorzPix = LOWORD(lParam);
  115.             WindVertPix = HIWORD(lParam);
  116.             break;
  117.  
  118.         case WM_ERASEBKGND:
  119.             return TRUE;
  120.  
  121.         case WM_PAINT:
  122.             {
  123.                 PAINTSTRUCT ps;
  124.                 char szFormat[20];
  125.                 char szUnits[20];
  126.                 char szBuffer[60];
  127.                 float nHorz = (float)WindHorzPix;
  128.                 float nVert = (float)WindVertPix;
  129.                 RECT rect;
  130.  
  131.                 if (CurrentUnits != ID_PIXELS) {
  132.                     nHorz *= (float)ScrnHorzMil / ScrnHorzPix;
  133.                     nVert *= (float)ScrnVertMil / ScrnVertPix;
  134.                 }
  135.                 if (CurrentUnits == ID_INCHES) {
  136.                     nHorz /= 25.4;
  137.                     nVert /= 25.4;
  138.                 }
  139.                 BeginPaint(hWnd, (LPPAINTSTRUCT)&ps);
  140.                 PatBlt(ps.hdc, 0, 0, WindHorzPix, WindVertPix,
  141.                  (CurrentColor == ID_WHITE) ? BLACKNESS : WHITENESS);
  142.                 if (CurrentColor == ID_WHITE) {
  143.                     SetTextColor(ps.hdc, RGB(255, 255, 255));
  144.                     SetBkColor(ps.hdc, RGB(0, 0, 0));
  145.                 }
  146.                 LoadString(hInst, IDS_FORMAT, (LPSTR)szFormat, 20);
  147.                 LoadString(hInst, CurrentUnits, (LPSTR)szUnits, 20);
  148.                 rect.bottom = WindVertPix - (rect.top = CharVertPix);
  149.                 rect.right = WindHorzPix - (rect.left = CharHorzPix);
  150.                 DrawText(ps.hdc, (LPSTR)szBuffer,
  151.                  sprintf(szBuffer, szFormat, nHorz, szUnits, nVert, szUnits),
  152.                  (LPRECT)&rect, DT_CENTER | DT_WORDBREAK);
  153.                 EndPaint(hWnd, (LPPAINTSTRUCT)&ps);
  154.             }
  155.             break;
  156.  
  157.         case WM_SYSCOMMAND:
  158.             switch (wParam) {
  159.                 case IDM_ABOUT:
  160.                     DialogBox(hInst, (LPSTR)"AboutBox", hWnd, lpAbout);
  161.                     break;
  162.  
  163.                 default:
  164.                     return DefWindowProc(hWnd, message, wParam, lParam);
  165.             }
  166.             break;
  167.  
  168.         case WM_COMMAND:
  169.             switch (wParam) {
  170.                 case ID_BLACK:
  171.                 case ID_WHITE:
  172.                     hMenu = GetMenu(hWnd);
  173.                     CheckMenuItem(hMenu, CurrentColor, MF_UNCHECKED);
  174.                     CheckMenuItem(hMenu, CurrentColor = wParam, MF_CHECKED);
  175.                     InvalidateRect(hWnd, (LPRECT)NULL, TRUE);
  176.                     break;
  177.  
  178.                 case IDM_UNITS:
  179.                     if (DialogBox(hInst, (LPSTR)"UnitsBox", hWnd, lpUnits))
  180.                         InvalidateRect(hWnd, (LPRECT)NULL, TRUE);
  181.                     break;
  182.  
  183.                 default:
  184.                     return DefWindowProc(hWnd, message, wParam, lParam);
  185.             }
  186.             break;
  187.  
  188.         case WM_DESTROY:
  189.             PostQuitMessage(0);
  190.             break;
  191.  
  192.         default:
  193.             return DefWindowProc(hWnd, message, wParam, lParam);
  194.             break;
  195.     }
  196.     return (0L);
  197. }
  198.  
  199. BOOL FAR PASCAL AboutWndProc(hDlg, message, wParam, lParam)
  200. HWND hDlg;
  201. unsigned message;
  202. WORD wParam;
  203. LONG lParam;
  204. {
  205.     switch (message) {
  206.         case WM_INITDIALOG:
  207.             return TRUE;
  208.  
  209.         case WM_COMMAND:
  210.             EndDialog(hDlg, TRUE);
  211.             return TRUE;
  212.  
  213.         default:
  214.             return FALSE;
  215.     }
  216. }
  217.  
  218. BOOL FAR PASCAL UnitsWndProc(hDlg, message, wParam, lParam)
  219. HWND hDlg;
  220. unsigned message;
  221. WORD wParam;
  222. LONG lParam;
  223. {
  224.     static int NewUnits;
  225.  
  226.     switch (message) {
  227.         case WM_INITDIALOG:
  228.             CheckRadioButton(hDlg, ID_PIXELS, ID_INCHES, CurrentUnits);
  229.             SetFocus(GetDlgItem(hDlg, NewUnits = CurrentUnits));
  230.             return FALSE;
  231.  
  232.         case WM_COMMAND:
  233.             switch (wParam) {
  234.                 case ID_MILLIM:
  235.                 case ID_INCHES:
  236.                 case ID_PIXELS:
  237.                     NewUnits = wParam;
  238.                     CheckRadioButton(hDlg, ID_PIXELS, ID_INCHES, NewUnits);
  239.                     break;
  240.  
  241.                 case IDOK:
  242.                     CurrentUnits = NewUnits;
  243.                     EndDialog(hDlg, TRUE);
  244.                     break;
  245.  
  246.                 case IDCANCEL:
  247.                     EndDialog(hDlg, FALSE);
  248.                     break;
  249.                     
  250.                 default:
  251.                     return FALSE;
  252.             }
  253.             break;
  254.  
  255.         default:
  256.             return FALSE;
  257.     }
  258.     return TRUE;
  259. }
  260.